package brcl.handler;

import brcl.exception.NoteException;
import brcl.view.ServerResponse;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常返回
 *
 * @version v1.0
 * @className: GlobalExceptionHandler
 * @author: heyiheng
 * @date: 2021-04-16 16:54
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

	@ExceptionHandler(value = {Exception.class})
	public ServerResponse handleNoteException(Exception e) {

		if (e instanceof NoteException) {
			int code = ((NoteException) e).getCode();
			if (code != 0) {
				return ServerResponse.createByErrorCodeMessage(code,e.getMessage());
			}
			return ServerResponse.createByErrorMessage(e.getMessage());
		}
		if (e instanceof HttpRequestMethodNotSupportedException){
			return ServerResponse.createByErrorMessage("不接受此类请求!");
		}
		e.printStackTrace();
		//return ServerResponse.createByErrorMessage(e.toString());
		return ServerResponse.createByErrorMessage("服务器繁忙请稍后再试!");
	}
}
